<template>
  <div>
    <div class="perMana">
      <el-input size="small" placeholder="请输入角色英文名称"
                v-model="role.name">
      <template  slot="prepend" style="width: 20%">ROLE_ </template>
      </el-input>
      <el-input size="small" placeholder="请输入角色中文名称"
                v-model="role.nameZh" @keydown.enter.native>
      </el-input>
      <el-button type="primary" size="mini" icon="el-icon-plus" @click="Add">
        添加角色
      </el-button>
    </div>
    <div class="perManaMain">
      <el-collapse v-model="activeName" accordion @change="change">
        <el-collapse-item :title="r.nameZh" :name="r.id" v-for="(r,index) in roles" :key="index">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>可访问的资源</span>
              <el-button style="width:10%;float: right; padding: 3px;margin-left: 5px" type="danger" icon="el-icon-delete" @click="delRole(r)"></el-button>
            </div>
            <div>
              <el-tree :data="allmenus"
                       :props="defaultProps"
                       ref="tree"
                       node-key="id"
                       :key="index"
                       :default-checked-keys="selectMenus"
                       show-checkbox>
              </el-tree>
              <div style="display:flex;justify-content: flex-end;margin-top: 5px">
                <el-button size="mini" type="info" @click="cancelUpdate">取消修改</el-button>
                <el-button size="mini" type="primary" @click="doUpdate(r.id,index)">确认修改</el-button>
              </div>
            </div>
          </el-card>
        </el-collapse-item>
      </el-collapse>
    </div>
  </div>
</template>

<script>
export default {
  name: "PerManager",
  data(){
    return{
      role:{
        name:'',
        nameZh:'',
      },
      activeName:-1,
      roles:[],
      allmenus:[],
      selectMenus:[],
      defaultProps:{
        children:'children',
        label:'name'
      }
    }
  },
  methods:{
    initRoles(){
      this.getRequest("/system/basic/perms/").then(resp=>{
        if(resp){
          this.roles=resp;
        }
      })
    },
    initMenus(){
      this.getRequest("/system/basic/perms/menus").then(resp=>{
        if(resp){
          this.allmenus=resp;
        }
      })
    },
    initSelectMenus(rid){
      this.getRequest("/system/basic/perms/mids/"+rid).then(resp=>{
        if(resp){
          this.selectMenus=resp;
        }
      })
    },
    change(rid){
      if(rid){
        this.initMenus();
        this.initSelectMenus(rid);
      }
    },
    doUpdate(rid,index){
      let tree = this.$refs.tree[index];
      let selectedKeys = tree.getCheckedKeys(true);
      let url = '/system/basic/perms/?rid='+rid;
      selectedKeys.forEach(key=>{
        url+='&mids=' + key;
      })
      this.putRequest(url).then(resp=>{
        if(resp){
          this.activeName = -1;
        }
      })
    },
    cancelUpdate(){
      this.activeName=-1;
    },
    Add(){
      if(this.role.name&&this.role.nameZh){
        this.postRequest("/system/basic/perms/",this.role).then(resp=>{
          if(resp){
            this.role.nameZh='';
            this.role.name='';
            this.initRoles();
          }
        })
      }else{
        this.$message.error("中文名称和英文名称不能为空！")
      }

    },
    delRole(role){
      this.$confirm('此操作将永久删除'+role.nameZh+', 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest("/system/basic/perms/"+role.id).then(resp=>{
          if(resp){
            this.initRoles();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  },
  mounted() {
    this.initRoles();
    this.activeMenu=-1;
  }
}
</script>

<style>
.perMana {
  display: flex;
  justify-content: flex-start;
}
.perMana .el-input{
  width: 300px;
  margin-right: 5px;
}
.perManaMain{
  margin-top: 10px;
  width: 720px;
}

</style>